package org.wendu.wdoa.common;


import lombok.extern.slf4j.Slf4j;
import org.wendu.wdoa.exception.BusinessException;
import org.wendu.wdoa.exception.SysException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RestControllerAdvice//该注解说明本类是一个restconterllor的异常拦截者
public class DemoControllerAdvice {


    @ExceptionHandler(ConstraintViolationException.class)//该注解说明本方法是一个异常处理器
    public Result handleConstrainViolationException(ConstraintViolationException e){

        log.error("参数不匹配",e);
        Set<ConstraintViolation<?>> set =     e.getConstraintViolations();//获取验证错误集合
        //将验证错误集合中每一个错误的信息取出来组成一个新的集合，再将新集合中的元素用，分割连接成一个字符串
        String msg = set.stream().map(item->item.getMessage()).collect(Collectors.joining(","));
//        String msg1 = msg.substring(0,msg.length()-1);
        return Result.err(Result.CODE_ERR_SYS,msg);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result MethodArgumentNotValidException(MethodArgumentNotValidException e){
        log.error("参数不匹配",e);
        BindingResult bindingResult = e.getBindingResult(); //获取参数绑定的结果（包括绑定错误信息）
        List<FieldError> fieldErrorList = bindingResult.getFieldErrors();
        String mes = fieldErrorList.stream().map(fieldError -> fieldError.getField()+":"+fieldError.getDefaultMessage())
        .collect(Collectors.joining(","));
        return Result.err(Result.CODE_ERR_SYS,mes);
    }
    @ExceptionHandler(BindException.class)
    public Result handleBindException(BindException e){
        log.error("参数不匹配",e);
        BindingResult bindingResult = e.getBindingResult(); //获取参数绑定的结果（包括绑定错误信息）
        List<FieldError> fieldErrorList = bindingResult.getFieldErrors();
        String mes = fieldErrorList.stream().map(fieldError -> fieldError.getField()+":"+fieldError.getDefaultMessage())
                .collect(Collectors.joining(","));
        return Result.err(Result.CODE_ERR_SYS,mes);
    }

    @ExceptionHandler(SysException.class) //处理自定义系统异常
    public Result handleSysException(SysException e){
        log.error("系统错误!",e);
        return Result.err(Result.CODE_ERR_SYS,"系统升级中...");

    }

    @ExceptionHandler(BusinessException.class) //处理自定义业务异常
    public Result handleBusinessException(BusinessException e){
        return Result.err(Result.CODE_ERR_BUSINESS,e.getMessage());

    }

    @ExceptionHandler(TypeMismatchException.class) //参数格式异常
    public Result handleTypeMismatchException(TypeMismatchException e){
        log.error("系统错误",e);
        return Result.err(Result.CODE_ERR_SYS,"系统升级中...");

    }

    @ExceptionHandler(Exception.class) //处理遗漏的异常
    public Result handleException(Exception e){
        log.error("系统错误",e);
        return Result.err(Result.CODE_ERR_SYS,"系统升级中...");

    }

}
